/**
 * 
 */
package com.sj.szxy.genetic.algorithm.basic;

/**
 * @author Yi Ping
 * @date 2018年4月3日 下午6:50:12 
 * @since 1.0.0
 *
 */
public class Individual {
	private int[] chromosome;
	private double fitness = -1f;
	
	/**
	 * @param chromosome
	 */
	public Individual(int[] chromosome) {
		this.chromosome = chromosome;
	}
	
	/**
	 * 
	 * @param chromosomeLength
	 */
	public Individual(int chromosomeLength) {
		this.chromosome = new int[chromosomeLength];
		for(int gene = 0; gene <chromosomeLength; gene++) {
			if(0.5< Math.random())
				this.setGene(gene,1);
			else
				this.setGene(gene,0);
		}
	}

	public int[] getChromosome() {
		return chromosome;
	}

	public double getFitness() {
		return fitness;
	}
	
	public void setFitness(double fitness) {
		this.fitness = fitness;
	}

	public int getChromosomeLength() {
		if(this.chromosome == null)
			return 0;
		return this.chromosome.length;
	}
	
	public void setGene(int offset, int gene) {
		this.chromosome[offset] = gene;
	}
	
	public int getGene(int offset) {
		return this.chromosome[offset];
	}

	@Override
	public String toString() {
		StringBuilder output = new StringBuilder();
		for(int gene = 0; gene < this.getChromosomeLength(); gene ++) {
			output.append(this.getGene(gene));
		}
		
		return output.toString();
	}
	
	
}
